﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Odelle.WoWArmory;
using Odelle.WoWArmory.Data;

namespace Odelle.BlueForest
{
    public partial class FormMain : Form
    {
        Odelle.WoWArmory.Armory Armory = new Armory();
        FilterBinder binder = new FilterBinder();

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            initFilterItems();

            splitContainerMain.Dock = DockStyle.Fill;
            textBox1.Dock = DockStyle.Fill;
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.Visible = false;

            cmbRegion.Items.Clear();
            cmbRegion.Items.AddRange(Armory.Characters.GetRegionNames());
            cmbRegion.SelectedIndex = 0;

            Armory.SetCallbackMessenger(CallbackMessage);
        }

        private void initFilterItems()
        {
            binder.Add(filterItemGuild);

            binder.Add(filterItem1);
            binder.Add(filterItem2);
            binder.Add(filterItem3);

            for (int i = 4; i <= 20; ++i)
            {
                FilterItem fitem = new FilterItem();
                this.flowLayoutPanel1.Controls.Add(fitem);
                fitem.FilterName = "";
                fitem.Width = filterItem1.Width;
                fitem.TitleWidth = filterItem1.TitleWidth;

                fitem.Name = "filterItem" + i.ToString();

                binder.Add(fitem);
            }

            binder.UpdateNames();

            binder.FilterUpdated += new System.EventHandler(this.binder_FilterUpdated);
        }

        public void CallbackMessage(string message)
        {
            stripLabelDataInfo.Text = message;
            System.Windows.Forms.Application.DoEvents();
        }

        private void cmbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateRealms();
        }

        private void cmbRealm_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdatePrefilter();
            UpdateFilters();
            UpdateCharacters();
        }

        private void cmbRealm_Leave(object sender, EventArgs e)
        {
            UpdatePrefilter();
            UpdateFilters();
            UpdateCharacters();
        }

        private void binder_FilterUpdated(object sender, EventArgs e)
        {
            UpdateCharacters();
        }

        private void cmbCharacter_SelectedIndexChanged(object sender, EventArgs e)
        {
            Odelle.WoWArmory.Data.CharacterData chara = Armory.GetCharacter(cmbRegion.Text, cmbRealm.Text, cmbCharacter.Text, Armory.TypeAccessFrequency.FrequencyNever);
            ShowData(chara);
        }

        private void UpdateRealms()
        {
            cmbRealm.Items.Clear();
            string[] names = Armory.Characters.GetRealmNames(cmbRegion.Text);
            cmbRealm.Items.AddRange(names);

            if (cmbRealm.Items.Count > 0)
            {
                if(cmbRealm.Text == "" )
                    cmbRealm.SelectedIndex = 0;
                else if (!cmbRealm.Items.Contains(cmbRealm.Text))
                    cmbRealm.SelectedIndex = 0;
            }
        }

        private void UpdatePrefilter()
        {
            binder.Prefilter = "Region" + binder.QueryDelimiter + cmbRegion.Text + binder.QuerySeparator+ "Realm" + binder.QueryDelimiter + cmbRealm.Text;
            binder.UpdateNames();
        }

        private void UpdateCharacters()
        {
            cmbCharacter.Items.Clear();
            string[] names=null;

            names = binder.GetValuesList("Character");
            cmbCharacter.Items.AddRange(names);

            if (cmbCharacter.Items.Count > 0)
            {
                if (cmbCharacter.Text == "")
                    cmbCharacter.SelectedIndex = 0;
                else if (!cmbCharacter.Items.Contains(cmbCharacter.Text))
                    cmbCharacter.SelectedIndex = 0;
            }

            stripLabelDataInfo.Text = cmbCharacter.Items.Count.ToString() + " characters";

            if (chkViewAll.Checked)
            {
                CharacterData[] list = binder.GetCharacterDataList();
                ShowData(list);
            }
        }

        private bool stopUpdateFilters = false;
        private void UpdateFilters()
        {
            if (stopUpdateFilters)
                return;

            binder.AttachCharacterDataSet(Armory.Characters.CharacterDB.Values.ToArray<Odelle.WoWArmory.Data.CharacterData>());

            binder.Prefilter = "Region" + binder.QueryDelimiter + cmbRegion.Text + binder.QuerySeparator + "Realm" + binder.QueryDelimiter + cmbRealm.Text;
            binder.UpdateValues();
        }

        private void btnClearFilters_Click(object sender, EventArgs e)
        {
            binder.ClearQueryValues();
            UpdateFilters();
            UpdateCharacters();
        }

        private void btnFetch_Click(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
            System.Globalization.TextInfo tInfo = culture.TextInfo;

            Odelle.WoWArmory.Data.CharacterData chara=   Armory.GetCharacter(cmbRegion.Text ,cmbRealm.Text, cmbCharacter.Text,Armory.TypeAccessFrequency.FrequencyAlways);

            if (chara != null)
            {
                cmbRealm.Text = chara.Realm;
                cmbCharacter.Text = chara.Character;
            }

            UpdateRealms();
            UpdateFilters();
            UpdateCharacters();
            
            ShowData(chara);
        }

        private void btnCrawlGuild_Click(object sender, EventArgs e)
        {
            if (filterItemGuild.Value == "*" || filterItemGuild.Value.Trim() == "")
                return;

            int count = Armory.CrawlGuild(cmbRegion.Text, cmbRealm.Text, filterItemGuild.Value);

            UpdateFilters();
            UpdateCharacters();
        }

        private void btnUpdateAll_Click(object sender, EventArgs e)
        {
            Armory.UpdateAllCharacters();
            UpdateFilters();
            UpdateCharacters();
        }

        private void chkViewAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkViewAll.Checked)
            {
                textBox1.Visible = false;
                dataGridView1.Visible = true;
            }
            else
            {
                textBox1.Visible = true;
                dataGridView1.Visible = false;
            }
            UpdateCharacters();
        }

        private void ShowData(CharacterData[] charaList)
        {
            List<CharacterData> list = charaList.ToList<CharacterData>();
            BindingSource source = new BindingSource();
            source.DataSource = list;

            dataGridView1.DataSource = source;
        }

        private void ShowData(CharacterData chara)
        {
            string text = "";

            if (chara == null)
            {
                text = "null data exception";
            }
            else
            {
                text = text + " System" + "\r\n";
                text = text + "   " + "Version" + ": " + chara.Version.ToString() + "\r\n";
                text = text + "   " + "LastLogin" + ": " + chara.LastLogin.ToString() + "\r\n";
                text = text + "   " + "Updated" + ": " + chara.LastUpdated.ToString() + "\r\n";
                text = text + "   " + "ErrCode" + ": " + chara.ErrCode.ToString() + "\r\n";
                text = text + "Identify" + "\r\n";
                text = text + "   " + "Region" + ": " + chara.Region.ToString() + "\r\n";
                text = text + "   " + "Realm" + ": " + chara.Realm.ToString() + "\r\n";
                text = text + "   " + "Character" + ": " + chara.Character.ToString() + "\r\n";
                text = text + "   " + "Title" + ": " + chara.Title.ToString() + "\r\n";
                text = text + "   " + "ClassName" + ": " + chara.ClassName.ToString() + "\r\n";
                text = text + "   " + "Spec1" + ": " + chara.Spec1.ToString() + "\r\n";
                text = text + "   " + "Spec2" + ": " + chara.Spec2.ToString() + "\r\n";
                text = text + "   " + "Race" + ": " + chara.Race.ToString() + "\r\n";
                text = text + "   " + "Guild" + ": " + chara.Guild.ToString() + "\r\n";
                text = text + "\r\n";
                text = text + "   " + "Level" + ": " + chara.Level.ToString() + "\r\n";
                text = text + "   " + "Achieve" + ": " + chara.Achievement.ToString() + "\r\n";
                text = text + "Items" + "\r\n";
                text = text + "   " + "iLvlEquip" + ": " + chara.ItemLevelEquip.ToString() + "\r\n";
                text = text + "   " + "iLvlAve" + ": " + chara.ItemLevel.ToString() + "\r\n";
                text = text + "Professions" + "\r\n";
                text = text + "   " + "Prof1" + ": " + chara.Profession1.ToString() + "\r\n";
                text = text + "   " + "Prof1val" + ": " + chara.Profession1Value.ToString() + "\r\n";
                text = text + "   " + "Prof2" + ": " + chara.Profession2.ToString() + "\r\n";
                text = text + "   " + "Prof2val" + ": " + chara.Profession2Value.ToString() + "\r\n";
                text = text + "Base stats" + "\r\n";
                text = text + "   " + "Health" + ": " + chara.BaseHealth.ToString() + "\r\n";
                text = text + "   " + "Mana" + ": " + chara.BaseMana.ToString() + "\r\n";
                text = text + "   " + "Str" + ": " + chara.BaseStrength.ToString() + "\r\n";
                text = text + "   " + "Sta" + ": " + chara.BaseStamina.ToString() + "\r\n";
                text = text + "   " + "Agi" + ": " + chara.BaseAgility.ToString() + "\r\n";
                text = text + "   " + "Int" + ": " + chara.BaseIntellegence.ToString() + "\r\n";
                text = text + "   " + "Spi" + ": " + chara.BaseSpirit.ToString() + "\r\n";
                text = text + "   " + "Mastery" + ": " + chara.BaseMastery.ToString() + "\r\n";
                text = text + "Spell attack" + "\r\n";
                text = text + "   " + "SplPower" + ": " + chara.SpellPower.ToString() + "\r\n";
                text = text + "   " + "SplHaste" + ": " + chara.SpellHaste.ToString() + "\r\n";
                text = text + "   " + "SplHit" + ": " + chara.SpellHit.ToString() + "\r\n";
                text = text + "   " + "SplCrit" + ": " + chara.SpellCrit.ToString() + "\r\n";
                text = text + "   " + "ManaRegen" + ": " + chara.ManaRegenRest.ToString() + "\r\n";
                text = text + "   " + "CombatRegen" + ": " + chara.ManaRegenCombat.ToString() + "\r\n";
                text = text + "Melee attack" + "\r\n";
                text = text + "   " + "MelPower" + ": " + chara.MeleePower.ToString() + "\r\n";
                text = text + "   " + "MelDps" + ": " + chara.MeleeDps.ToString() + "\r\n";
                text = text + "   " + "MelDps2" + ": " + chara.MeleeDps2.ToString() + "\r\n";
                text = text + "   " + "MelSpeed" + ": " + chara.MeleeSpeed.ToString() + "\r\n";
                text = text + "   " + "MelSpeed2" + ": " + chara.MeleeSpeed2.ToString() + "\r\n";
                text = text + "   " + "MelHit" + ": " + chara.MeleeHit.ToString() + "\r\n";
                text = text + "   " + "MelCrit" + ": " + chara.MeleeCrit.ToString() + "\r\n";
                text = text + "   " + "MelHaste" + ": " + chara.MeleeHaste.ToString() + "\r\n";
                text = text + "   " + "MelHaste2" + ": " + chara.MeleeHaste2.ToString() + "\r\n";
                text = text + "   " + "Expertise" + ": " + chara.Expertise.ToString() + "\r\n";
                text = text + "   " + "Expertise2" + ": " + chara.Expertise2.ToString() + "\r\n";
                text = text + "Ranged attack" + "\r\n";
                text = text + "   " + "RngPower" + ": " + chara.RangePower.ToString() + "\r\n";
                text = text + "   " + "RngDps" + ": " + chara.RangeDps.ToString() + "\r\n";
                text = text + "   " + "RngSpeed" + ": " + chara.RangeSpeed.ToString() + "\r\n";
                text = text + "   " + "RngHit" + ": " + chara.RangeHit.ToString() + "\r\n";
                text = text + "   " + "RngCrit" + ": " + chara.RangeCrit.ToString() + "\r\n";
                text = text + "   " + "RngHaste" + ": " + chara.RangeHaste.ToString() + "\r\n";
                text = text + "defence" + "\r\n";
                text = text + "   " + "Armor" + ": " + chara.DefenceArmor.ToString() + "\r\n";
                text = text + "   " + "Dodge" + ": " + chara.DefenceDodge.ToString() + "\r\n";
                text = text + "   " + "Parry" + ": " + chara.DefenceParry.ToString() + "\r\n";
                text = text + "   " + "Block" + ": " + chara.DefenceBlock.ToString() + "\r\n";
                text = text + "   " + "Resili" + ": " + chara.Resilience.ToString() + "\r\n";
                text = text + "   " + "PvPPower" + ": " + chara.PvpPower.ToString() + "\r\n";
            }

            textBox1.Text = text;
        }

    }
}
